#import "OmHandyMethods.h"
#import <CommonCrypto/CommonDigest.h>
@implementation OmHandyMethods
+ (UIImage *)imageFromColor:(UIColor *)fromColor gradientToColor:(UIColor *)toColor frame:(CGRect)frame {
    CAGradientLayer *gradientLayer = [CAGradientLayer layer];
    gradientLayer.frame = frame;
    gradientLayer.colors = @[(__bridge id)fromColor.CGColor,(__bridge id)toColor.CGColor];
    gradientLayer.locations = @[@(0.0),@(1.0f)];
    gradientLayer.startPoint = CGPointMake(0, 0);
    gradientLayer.endPoint = CGPointMake(1, 1);
    UIGraphicsBeginImageContext(gradientLayer.frame.size);
    [gradientLayer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}
+ (UIImage *)getRandomHeadImage {
    int randomID = [self getRandomNumberFrom:7 to:7];
    NSString *randomImageName = [NSString stringWithFormat:@"defaultHead%d", randomID];
    return IMAGE_NAMED(randomImageName);
}
+ (int)getRandomNumberFrom:(int)from to:(int)to {
    return (int)(from + (arc4random() % (to - from + 1)));
}
+ (CGFloat)getLabelHeightWithText:(NSString *)text width:(CGFloat)width font:(UIFont *)font {
    CGRect rect = [text boundingRectWithSize:CGSizeMake(width, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:font} context:nil];
    return rect.size.height;
}
+ (NSString *)md5:(NSString *)string {
    const char *cStr = [string UTF8String];
    unsigned char result[16];
    CC_MD5(cStr, (CC_LONG)strlen(cStr), result);
    NSString * md5Str = [NSString stringWithFormat:
                         @"%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
                         result[0], result[1], result[2], result[3],
                         result[4], result[5], result[6], result[7],
                         result[8], result[9], result[10], result[11],
                         result[12], result[13], result[14], result[15]
                         ];
    return md5Str;
}
+ (NSString *)resizedImageUrlwith:(NSString *)url width:(NSNumber *)width height:(NSNumber *)height {
    NSMutableString *appendString = [NSMutableString stringWithString:@"?imageView2/0"];
    if (width) [appendString appendString:[NSString stringWithFormat:@"/w/%@", width]];
    if (height) [appendString appendString:[NSString stringWithFormat:@"/h/%@", height]];
    return [url stringByAppendingString:appendString];
}
+ (NSString *)dateStringFromTimeStamp:(NSTimeInterval)timeStamp {
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:timeStamp];
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm"];
    return [formatter stringFromDate:date];
}
@end
